package com.unicorn;


import com.unicorn.server.WebServer;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import java.util.Map;

/**
 * @Description
 * @Author Meng.shen
 * @Date 2022/12/6
 **/
public class MyApplication {

    public static void run(Class clazz) {
        //要访问到UserController-》需要启动tomcat=》
        //我们需要dispatchServlet来转发请求=》从spring中获取到对应的controller
        //创建一个spring容器
        //将MyBootSampleApp注册到spring容器中，这样就会解析MyBootSampleApp上面的注解
        //解析到MyBootConfiguration注解里面的@ComponentScan注解，
        // 就能扫描到MyBootSampleApp所在包和子包中的带有@RestController和@Service注解的类
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(clazz);
        context.refresh();

        //启动tomcat 问题：如果我们不想用tomcat 想用jetty呢？
        //如果项目中有Tomcat的依赖，那就启动Tomcat
        //如果项目中有Jetty的依赖就启动Jetty
        //如果两者都没有则报错
        //如果两者都有也报错
        //startTomcat(context);

        WebServer webServer = getWebServer(context);
        webServer.start(context);
    }

    private static WebServer getWebServer(AnnotationConfigWebApplicationContext context) {
        //判断返回是TomcatServer还是JettyServer
        //根据条件注解 以及是否引入了其依赖进行判断
        Map<String, WebServer> webServers = context.getBeansOfType(WebServer.class);
        if(webServers.isEmpty()){
            throw new NoSuchBeanDefinitionException("no such bean exception");
        }
        if(webServers.size() > 1){
            throw new IllegalStateException("bean number more than one");
        }
        return webServers.values().stream().findFirst().get();
    }

    /*private static void startTomcat(WebApplicationContext applicationContext) {
        Tomcat tomcat = new Tomcat();

        Server server = tomcat.getServer();
        Service service = server.findService("Tomcat");

        Connector connector = new Connector();
        connector.setPort(8081);

        Engine engine = new StandardEngine();
        engine.setDefaultHost("localhost");

        Host host = new StandardHost();
        host.setName("localhost");

        String contextPath = "";
        Context context = new StandardContext();
        context.setPath(contextPath);
        context.addLifecycleListener(new Tomcat.FixContextListener());

        host.addChild(context);
        engine.addChild(host);

        service.setContainer(engine);
        service.addConnector(connector);

        tomcat.addServlet(contextPath, "dispatcher", new DispatcherServlet(applicationContext));
        context.addServletMappingDecoded("/*", "dispatcher");

        try {
            tomcat.start();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }

    }*/
}
